package com.googlecode.accurev2disk;

import java.io.File;

public class AccurevStream {

	def String name
	def AccurevStream basicStream
	def int transactionNumber
	def boolean created

	
	public AccurevStream(String name, AccurevStream basicStream, int transactionNumber) {
		super();
		this.name = name;
		this.basicStream = basicStream;
		this.transactionNumber = transactionNumber;
		this.created = false}

	public AccurevStream(String name){
		super()
		this.name = name
	}
	
	public String getName(){
		return name
	}

	public AccurevStream(String name, String basicName, int transactionNumber){
		super()
		this.name = name
		this.basicStream = new AccurevStream(basicName)
		this.transactionNumber = transactionNumber
	}



	/**
	 * creates this Stream as a real Stream in Accurev
	 */
	public void createInAccurev(){


		def Process proc
		//		 create Accurev command and execute
//		 without given basicStream only create a new Stream		
		if (basicStream == null){
			def mts = "accurev mkstream -s " + name	
			proc = mts.execute()
			proc.waitFor()
		} else {			
			def mts = "accurev mkstream -s " + name + " -b " + basicStream.name + " -t " + transactionNumber		
			proc = mts.execute()
			proc.waitFor()
		}
		
//		write results to console
		if(proc.exitValue() == 0) {
			println "Stream ${name} created"
			created = true
		} else {
		println "Stream ${name} not created. reason: ${proc.err.text}"
		}

	}


	/**
	 * changes the Stream to another transaction of another basic stream
	 * @param basicStream new basicStream for this Stream
	 * @param tNumber new TransactionNumber for this Stream
	 */
	public void change(AccurevStream basicStream, int tNumber){

		//		create Accurev command and execute
		def cts = "accurev chstream -s " + name + " -b " + basicStream.name + " -t " + tNumber
		def proc = cts.execute()
		
			String err = proc.getErr()
			String text = proc.getText()
			
		proc.waitFor()		

		if(proc.exitValue() == 0) {
			println "Stream ${name} changed to ${basicStream.name} at # ${tNumber}"
			this.basicStream = basicStream
			this.transactionNumber = tNumber
		} else {
		println "Stream ${name} not changed. reason: ${err} ${text}"
		}
	}

	/**
	 * changes the Stream to another basic stream
	 * @param basicStream new basicStream for this Stream
	 */
	public void change(AccurevStream stream){
		change(stream,this.transactionNumber)
	}

	/**
	 * changes the Stream to another transaction of the basic stream
	 * @param tNumber new TransactionNumber for this Stream
	 */
	public void change(int tNumber){
		change(this.basicStream, tNumber)
	}



	public void populateAt(int tNumber, File storepath){

		change(tNumber)
		
		storepath.mkdirs()

		// create Accurev command and execute
		def pts = "accurev pop -R -v " + name + " -L " + storepath.getPath() + " ."
		println "populateAt waitFor ${pts}"
		def proc = pts.execute()
		
		
				String err = proc.getErr()
				String text = proc.getText()
		
		proc.waitFor()

		if(proc.exitValue() == 0) {
			println "Stream ${name} populated at transaction # ${tNumber}\r\n"
			println "to ${storepath}"
		} else {
		println "Stream ${name} not populated. reason: ${proc.err.text} ${proc.text}\r\n"
		}

	}

	/** 
	 * Copies all the files of this stream at that transaction number to the storepath directory.
	 * storepath\tNumber\files
	 * @param tNumber transaction number
	 * @param storepath directory the files shall be stored at
	 */
	public void populateTo(File storepath){

		String path = storepath.getPath()
		new File(path).mkdirs()
			

		// create Accurev command and execute
		def pts = "accurev pop -R -v " + this.name + " -L " + path + " ."
		println "populateTo waitFor ${pts}"
		def proc = pts.execute()
		
		String err = proc.getErr()
		String text = proc.getText()
		
		proc.waitFor()

		if(proc.exitValue() == 0) {
			println "Stream ${name} populated at transaction # ${transactionNumber}\r\n"
			println "to ${path}"
		} else {
		println "Stream ${name} not populated. reason: ${err} ${text}\r\n"
		}

	}

	/** 
	 * Copies all the files of this stream at that transaction number to the storepath directory.
	 * storepath\tNumber\files
	 * @param tNumber transaction number
	 * @param storepath directory the files shall be stored at
	 */
	public void populateFilesTo(File storepath, File files){

		String path = storepath.getPath()
		new File(path).mkdirs()
			

		//create accurev pop command and execute
				//populate from the temp stream the files from version to the path
			def pts = "accurev pop -R -v ${name} -L ${storepath.getPath()} -l ${files.getAbsolutePath()}"
			println "populateFilesTo waitFor ${pts}"	
			def	proc = pts.execute()
				
			//read out the buffer (otherwise proc will not end)
			String err = proc.getErr()
			String text = proc.getText()

			//wait for the process to end
			proc.waitFor()


			if(proc.exitValue() == 0) {
				println "Stream ${name} populated at ${transactionNumber} to ${storepath.getPath()}\r\n"
			} else {
				println "Stream ${name} not populated at ${transactionNumber} reason: ${proc.err.text}\r\n"
			}

	}



	
	public String calculateHistory(){
		String text
		String err
		
			def hist = "accurev hist -s ${name} -a -fx"
			def proc = hist.execute()
            
			err = proc.getErr()
			text = proc.getText()

			if(proc.waitFor() == 0) {
			println "history for ${name} calculated."
			} else {
				println "failed calculating history for ${name}. reason: ${err}"
			}
			return text
			println "history stored at ${storefile.path}"
	}

	public String calculateHistory(int tNumber){
		String text
		String err
		
			def hist = "accurev hist -t ${tNumber}-now -s ${name} -a -fx"
			def proc = hist.execute()
            
			err = proc.getErr()
			text = proc.getText()

			if(proc.waitFor() == 0) {
			println "history for ${name} calculated."
			} else {
				println "failed calculating history for ${name}. reason: ${err}"
			}
			return text
			println "history stored at ${storefile.path}"
	}

	public String calculateMoved(){
		String text
		String err
		
			def hist = "accurev hist -p ${name} -a -k  move -fx"
			def proc = hist.execute()
            
			err = proc.getErr()
			text = proc.getText()

			if(proc.waitFor() == 0) {
			println "move history for ${name} calculated."
			} else {
				println "failed calculating history for ${name}. reason: ${err}"
			}
			return text
			println "history stored at ${storefile.path}"
	}

	public String calculateAdded(){
		String text
		String err
		
			def hist = "accurev hist -p ${name} -a -k add -fx"
			def proc = hist.execute()
            
			err = proc.getErr()
			text = proc.getText()

			if(proc.waitFor() == 0) {
			println "add history for ${name} calculated."
			} else {
				println "failed calculating history for ${name}. reason: ${err}"
			}
			return text
			println "history stored at ${storefile.path}"
	}

	public String calculateDeleted(){
		String text
		String err
		
			def hist = "accurev hist -p ${name} -a -k  defunct -fx"
			def proc = hist.execute()
            
			err = proc.getErr()
			text = proc.getText()

			if(proc.waitFor() == 0) {
			println "delete history for ${name} calculated."
			} else {
				println "failed calculating history for ${name}. reason: ${err}"
			}
			return text
			println "history stored at ${storefile.path}"
	}

	
	public String calculatePromoted(){
		String text
		String err
		
			def hist = "accurev hist -p ${name} -a -k  promote -fx"
			def proc = hist.execute()
            
			err = proc.getErr()
			text = proc.getText()

			if(proc.waitFor() == 0) {
			println "promote history for ${name} calculated."
			} else {
				println "failed calculating history for ${name}. reason: ${err}"
			}
			return text
			println "history stored at ${storefile.path}"
	}

	public String calculateMAD(){
		return calculateMoved() + calculateAdded() + calculateDeleted()
	}


	public String calculateDiff(AccurevStream to){

		//create Accurev command and execute
		def diff = "accurev diff -a -i -v " + this.name + " -V " + to.name		
		def proc = diff.execute()
		
			String err = proc.getErr()
			String text = proc.getText()
		
		proc.waitFor()

		println "Difference between ${name}-#${transactionNumber} and ${to.name}-#${to.transactionNumber} calculated\r\n"
		return text
			
		}

	
	
}
